#!C:\Python26

from pluginManage import Plugin
import re
import os
import sys
import BeautifulSoup as bs

import urllib
import urlparse
import webFunctions

import logmanager

class ThreadWalker(Plugin):

	#Configuration Variables - Loaded when plugin is selected
	capabilities		=	"wScraper"											#Used for detecting ifany random .py file is a plugin file
	version			=	1.2												#Used for detecting available configuration options
	pluginName		=	"BlogSpotScraper"										#Plugin Name variable
	siteName		=	"BlogSpot Image Scraper"									#Value for populating "Art Site" dropbox
	nameSource		=	["Single Thread", "Thread List File"]								#namesource value passed to plugin
	nameSourceEN		=	1												#Determines if Name Source input box is enabled (Boolean, 0-1)
	artistListEN		=	1												#Determines if Artist List input box is enabled (Boolean, 0-1)
	overwriteModes		=	["Check Files", "Overwrite Files"]								#Determines if overwrite mode dropbox is enabled for plugin
	retrievalHandles	=	1												#How many http retrieval handles app wants

	DLFolder		=	"BLogSpot Scraper"
	logFileName		=	"LogFile.txt"

	aNameLabeltxt		=	"Main Thread Page: "
	aListLabeltxt		=	"Number of Pages: "

	cookieCapable		=	0
	instructionText		=	"Nothing Yet"


	#log = logmanager.ThreadLog(retrievalHandles)

	def getThread(self, config, getHandles, blogRootName):											 #Main Function Call - Uses other functions


		self.DLFolderFull = os.path.join(self.DLFolder, blogRootName)

		if not os.access(self.DLFolderFull, os.W_OK):
				try:
					os.mkdir(self.DLFolderFull)
					pprint("Created folder "+self.DLFolderFull)
					pprint("Beginning Download")
				except:
					pprint("Cannot Make working directory ("+self.DLFolderFull+"/. Do you have write Permissions?", sys.exc_info()[0])
					return 0


		pagesRemaining = True
		imgLinks = []

		nextPageUrl = "%s%s%s" % ("http://", blogRootName, ".blogspot.com/")

		while pagesRemaining:

			pageText = getHandles.getpage(nextPageUrl)
			if pageText == "Failed":
				self.log.error("Could not get page. Failed")
				self.log.error("%s&page=%s" % (nextPageUrl))
				return 0

			soup = bs.BeautifulSoup(pageText)

			temp = soup.find('a', attrs={"class" : "blog-pager-older-link"})
			if temp:
				nextPageUrl = temp["href"]
			else:
				pagesRemaining = False


			posts = soup.findAll('div', attrs={"class" : "post-body entry-content"})
			for post in posts:
				images = post.findAll("a")
				for image in images:

					imgLinks.append(image["href"])



		print "Found %s links" % len(imgLinks)
		for link in imgLinks:

			pgContent, linkHandle = getHandles.getpage(link, returnMultiple = True)



			type = linkHandle.info()
			if type["content-type"].find("text/html") + 1:
				#image is on a sub-page


				#print "Content", pgContent
				sSoup = bs.BeautifulSoup(pgContent)
				subLink = sSoup.find("img")["src"]
				pgContent, linkHandle = getHandles.getpage(subLink, returnMultiple = True)

			fileName = link.rsplit("/")[-1]

			print len(pgContent), fileName

			filePath = os.path.join(self.DLFolderFull, fileName)
			if (not os.access(os.path.join(self.DLFolderFull, fileName), os.W_OK)) or config.ovwMode == "Overwrite Files" :


				fileHandle = open(filePath, "wb")
				fileHandle.write(pgContent)
				fileHandle.close()
			else:
				self.log.info("Already Have %s" % filePath)
			self.progBars.incSubBar()

		return 1

	def main(self, config, getHandles):											 #Main Function Call - Uses other functions
		self.log = logmanager.ThreadLog(self.retrievalHandles)

		progBars = webFunctions.ProgressBars()
		self.DLFolder = os.path.join(config.settings.downloadDir, self.DLFolder)

		if not os.access(self.DLFolder, os.W_OK):
				try:
					os.mkdir(self.DLFolder)
					self.log.info("Created folder "+self.DLFolder)
					self.log.info("Beginning Download")
				except:
					self.log.info("Cannot Make working directory ("+self.DLFolder+"/. Do you have write Permissions?", sys.exc_info()[0])
					return 0
		self.progBars = webFunctions.ProgressBars()

		self.progBars.setMainBarLen(len(config.aList))
		self.progBars.setMainBar(0)

		for name in config.aList:
			self.log.info(name)
			self.getThread(config, getHandles, name)
			self.progBars.incMainBar()

		logmanager.clearThreadLog(self.log)
